package com.qcby.teacher.controller;

import com.lowagie.text.DocumentException;
import com.qcby.teacher.config.TeachMessage;
import com.qcby.teacher.config.TeachSample1;
import com.qcby.teacher.entity.Teach;
import com.qcby.teacher.entity.TeachPlan;
import com.qcby.teacher.pdf.MarkdownDTO;
import com.qcby.teacher.pdf.MdToPdfUtils;
import com.qcby.teacher.service.TeachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/teach")
public class TeachController {

    @Autowired
    private TeachService teachService;



    @RequestMapping("/findAll")
    @ResponseBody
    public List<Teach> findAll(Integer page, Integer pageSize, String title){
        List<Teach> teachList = teachService.findAll(page,pageSize,title);
        System.out.println(teachList);
        return teachList;
    };

    @RequestMapping("/getCount")
    @ResponseBody
    public int getCount(Integer title) {

        return this.teachService.getCount(title);
    }

    @RequestMapping("/getPdfUrl")
    @ResponseBody
    public String getPdfUrl(Integer id) {

        return this.teachService.getPdfUrl(id);
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public int deleteById(int id){
        int temp = teachService.deleteById(id);
        System.out.println(temp);
        return temp;
    };

    @RequestMapping("/insertTeach")
    @ResponseBody
    public int insertTeach(Teach teach){
        Date date  = new Date();
        Teach teach1 = new Teach();
        teach1.setCreateTime(date);
        teach1.setName(teach.getName());
        teach1.setPath(teach.getPath());
        int temp = teachService.insertTeach(teach1);
        System.out.println(temp);
        return temp;
    };

    //测试添加信息是否能够传入到后端
    @RequestMapping("/test")
    @ResponseBody
    public String test( TeachPlan teachPlan) throws IOException, DocumentException {
//        request.setCharacterEncoding("utf-8");

//        //获取教师id的值
//        HttpSession session = request.getSession();
//        Integer teacher_id = (Integer) session.getAttribute("teacher_id");

        System.out.println(teachPlan);
        //将examMessage中的元素交给文心一言进行生成试卷
        TeachSample1 teachSample1 = new TeachSample1();
        String answer = teachSample1.wenxin(teachPlan.getQuestion());
        System.out.println(answer);
//        String generatexampapers = wenXinService.Generatexampapers(examMessage);
//        System.out.println(generatexampapers);

        // 提取试卷信息
        String exampaperUtil = TeachMessage.getTeachMessage(answer);

// 处理Markdown格式的换行符
        exampaperUtil = exampaperUtil.replace("\n", "  \n"); // 在行末添加两个空格表示换行

// 构建MarkdownDTO对象
        MarkdownDTO dto = MarkdownDTO.builder().content(exampaperUtil).title(teachPlan.getName()).build();

// 生成 PDF 文件
        List<MarkdownDTO> dtos = new ArrayList<>();
        dtos.add(dto);
        byte[] pdfBytes = TeachMessage.generatePdfFromMarkdowns(dtos);

// 保存 PDF 文件到指定路径
        String savePath = "D:\\teach\\" + teachPlan.getName() + ".pdf";
        File file = new File(savePath);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(pdfBytes);
        }

// 如果需要调试，可以打印生成的Markdown内容
        System.out.println("Markdown内容：" + exampaperUtil);

//        try {
//            MdToPdfUtils.convertPDF(dtos,teachPlan.getName(), response);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        examDao.insertToexampaper(examMessage)
        return savePath;
    }
}
